//
//  AttributedLabel.m
//  GeneralApp-master
//
//  Created by 福利购Mac on 16/11/24.
//  Copyright © 2016年 JASON. All rights reserved.
//

#import "AttributedLabel.h"

@implementation AttributedLabel
/**
 *  单个Label 修改
 **/
- (void)addAttributed:(NSString*)text rangeOfString:(NSString*)rangeSting fontSize:(CGFloat)fontSize fontColorHexString:(NSString*)textColorHexString{
    [self setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        if (!rangeSting) {
            return nil;
        }
        NSRange changeRange = [[mutableAttributedString string] rangeOfString:rangeSting options:NSCaseInsensitiveSearch];
        // Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
        UIFont *boldSystemFont = [UIFont systemFontOfSize:fontSize];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        if (font) {
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:changeRange];
            //设置可点击文本的颜色
            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor colorWithHexString:textColorHexString] CGColor] range:changeRange];
            CFRelease(font);
        }
        return mutableAttributedString;
    }];
}
/**
 *  单个Label 修改，带分割线（适用商品真实价格与市场价格）
 **/
- (void)addCutAttributed:(NSString*)text rangeOfString:(NSString*)rangeSting fontSize:(CGFloat)fontSize fontColorHexString:(NSString*)textColorHexString {
    [self setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        NSRange changeRange = [[mutableAttributedString string] rangeOfString:rangeSting options:NSCaseInsensitiveSearch];
        // Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
        UIFont *boldSystemFont = [UIFont systemFontOfSize:fontSize];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        if (font) {
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:changeRange];
            //设置可点击文本的颜色
            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor colorWithHexString:textColorHexString] CGColor] range:changeRange];
            [mutableAttributedString addAttribute:(NSString *)kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES]  range:changeRange];
            CFRelease(font);
        }
        return mutableAttributedString;
    }];
}

/**
 *  单个Label 粗体修改
 **/
- (void)addBoldAttributed:(NSString*)text rangeOfString:(NSString*)rangeSting fontSize:(CGFloat)fontSize fontColorHexString:(NSString*)textColorHexString{
    [self setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
        if (!rangeSting) {
            return nil;
        }
        NSRange changeRange = [[mutableAttributedString string] rangeOfString:rangeSting options:NSCaseInsensitiveSearch];
        // Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
        UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:fontSize];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        if (font) {
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:changeRange];
            //设置可点击文本的颜色
            [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor colorWithHexString:textColorHexString] CGColor] range:changeRange];
            CFRelease(font);
        }
        return mutableAttributedString;
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
